package com.mtsui.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;
import com.mtsui.client.PSRService;


/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class PaperScissorRockWeb implements EntryPoint{
  /**
   * This is the entry point method.
   */
  final static int PAPER = 0;
  final static int SCISSOR = 1;
  final static int ROCK = 2;
  
  RandomPlayer randomPlayer = new RandomPlayer();
  ManualPlayer manualPlayer = new ManualPlayer();
  
  int totalcounter = 0;
  int wincounter = 0;
  int losecounter = 0;
  
  public int judge(int p1, int p2) {
	if (p1 == p2) return 0;
	switch (p1) {
	case PAPER: if (p2 == ROCK)  return 1; break;
	case SCISSOR: if (p2 == PAPER) return 1; break; 
	case ROCK: if (p2 == SCISSOR) return 1; break; 
	}
    return -1;
  }
  private int processMove(int move, Label l)
  {
	  manualPlayer.SetNextMove(move);
	  int randomPlayerMove = randomPlayer.ProvideMove();
	  int result = judge(manualPlayer.ProvideMove(), 
			  randomPlayerMove);
	  manualPlayer.doLearn(move, randomPlayerMove, result);
	  randomPlayer.doLearn(randomPlayerMove, move, -result);
      totalcounter++;
	  if (result > 0) {
		  l.setText("Win!");
		  wincounter++;
	  } else if(result < 0) {
		  l.setText("Lose!");
		  losecounter++;
	  } else if (result == 0) {
		  l.setText("Draw!");
	  }
	  
	  p1win.setText(Integer.toString(wincounter));
	  p2win.setText(Integer.toString(losecounter));
	  return result;
  }
  
  final Label p1win = new Label();
  final Label p2win = new Label();
  private PSRServiceAsync psrService;
  
  public void onModuleLoad() {
	psrService = 
		(PSRServiceAsync) GWT.create(PSRService.class);
	ServiceDefTarget endpoint = (ServiceDefTarget) psrService;
	String moduleRelativeUrl = GWT.getModuleBaseURL() + "scores";
	endpoint.setServiceEntryPoint(moduleRelativeUrl);
	
	final AsyncCallback callback = new AsyncCallback() {

		public void onFailure(Throwable caught) {
			Window.alert("fail");
		}

		public void onSuccess(Object result) {
			Window.alert("Success");
		}		
	};
	psrService.FetchScore("hello", callback);
	final Button paper = new Button("Paper");
    final Button scissor = new Button("Scissor");
    final Button rock = new Button("Rock");
    
    final Label win = new Label();

    assert(judge(PAPER, SCISSOR) == 1);
    assert(judge(SCISSOR, PAPER) == -1);
    assert(judge(ROCK, ROCK) == 0);

    for(int x=0; x<10000; ++x) {
    	processMove(PAPER, win);
    }
    
    paper.addClickListener(new ClickListener() {
      public void onClick(Widget sender) {
        win.setText("Paper!");
        processMove(PAPER, win);
    	psrService.FetchScore("hello", callback);
      }
    });
    
    scissor.addClickListener(new ClickListener() {
        public void onClick(Widget sender) {
          win.setText("Scissor!");
          processMove(SCISSOR, win);
        }
      });
    
    rock.addClickListener(new ClickListener() {
        public void onClick(Widget sender) {
          win.setText("Rock!");
          processMove(ROCK, win);
        }
      });
    

    // Assume that the host HTML has elements defined whose
    // IDs are "slot1", "slot2".  In a real app, you probably would not want
    // to hard-code IDs.  Instead, you could, for example, search for all 
    // elements with a particular CSS class and replace them with widgets.
    //
    RootPanel.get("slot1").add(paper);
    RootPanel.get("slot2").add(scissor);
    RootPanel.get("slot3").add(rock);
    RootPanel.get("slot4").add(win);
    RootPanel.get("resultslot").add(win);
    RootPanel.get("p1score").add(p1win);
    RootPanel.get("p2score").add(p2win);
  }
  
  
}
